package com.apestech.framework.web.lib;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * @author hup
 * @create 2018-09-27-17:59
 */
public class Http {

    public enum ContentType {
        FROM(0), JSON(1);
        int value;
        ContentType(int value) {
            this.value = value;
        }
        public int getValue() {
            return value;
        }
    }

    private String url;
    private Map params;
    private String charset="UTF-8";
    private String contentType;
    private HttpURLConnection connection;
    private URL urlcon;

    public Http(String url, Map map){
        init(url, map, ContentType.JSON);
    }

    private void init(String url, Map params, ContentType dataType){
        this.url=url;
        this.params=params;
        setDataType(dataType);
    }

    public void setDataType(ContentType dataType){
        if(dataType==ContentType.FROM){
            this.contentType="application/x-www-form-urlencoded";
        } else if(dataType==ContentType.JSON){
            this.contentType="application/json";
        }
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String post() throws IOException {
        try {
            openConnect("POST");
            return  getOutput();
        } catch (IOException e) {
            throw e;
        }
    }

    public String get() throws IOException {
        try {
            openConnect("GET");
            return  getOutput();
        } catch (IOException e) {
            throw e;
        }
    }

    private void openConnect(String method) throws IOException {
        try {
            urlcon = new URL(url);// 创建连接
            connection = (HttpURLConnection) urlcon.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod(method); // 设置请求方式
            connection.setRequestProperty("Accept", contentType); // 设置接收数据的格式
            connection.setRequestProperty("Content-Type", contentType); // 设置发送数据的格式
            connection.connect();
        } catch (IOException e) {
            throw e;
        }
    }

    private String getOutput() throws IOException {
        StringBuffer sb=new StringBuffer();
        BufferedReader in = null;
        InputStream is = null;
        try {
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), charset); // utf-8编码
            out.append(JSON.toJSONString(params));
            out.flush();
            out.close();
            int code = connection.getResponseCode();
            if (code == 200) {
                is = connection.getInputStream();
            } else {
                is = connection.getErrorStream();
            }            // 读取响应
            in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            throw e;
        } finally {
            if(in!=null) in.close();
            if(is!=null) is.close();
        }
    }



}
